<template>
  <div :id="id" ref="boxwrap" :class="className" :style="{height:height,width:width}" :title="title" />
</template>

<script>
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '150px'
    },
    height: {
      type: String,
      default: '150px'
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null,
      fontColor: '#bebebe'
    }
  },
  mounted() {
    this.domResize()
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        title: [
          {
            text: this.title,
            left: 'center',
            top: 10,
            textStyle: {
              fontSize: 16,
              color: this.fontColor
            }
          }, {
            subtext: 'cpk:13\n中心值:23\n上/下限:10/30',
            right: '1%',
            top: '60%',
            subtextStyle: {
              fontSize: 16,
              fontWeight: 'normal',
              color: this.fontColor
            }
          }
        ],
        tooltip: {
          trigger: 'item'
        },
        series: [
          {
            name: '超标次数占比',
            type: 'pie',
            radius: '50%',
            center: ['50%', '40%'],
            label: {
              position: 'inner',
              formatter: '{d}%'
            },
            data: [
              { value: 20, name: 'CPK' },
              { value: 10, name: '中心值' },
              { value: 30, name: '上下限' }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    },
    domResize() {
      const that = this
      const dom = this.$refs.boxwrap
      this.$erd.listenTo(dom, function(element) {
        that.chart.resize()
      })
    }
  }
}
</script>
